/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.result.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.hanfu.domain.bo.HanfuBo;
import top.continew.admin.hanfu.domain.dto.HanfuDto;
import top.continew.admin.hanfu.domain.query.HanFuQuery;
import top.continew.admin.hanfu.domain.vo.HanfuVo;
import top.continew.admin.hanfu.domain.vo.wx.HanfuWxDeVo;
import top.continew.admin.hanfu.domain.vo.wx.HanfuWxVo;
import top.continew.admin.hanfu.service.impl.HanfuServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-03-20
 */
@RestController
@RequestMapping("/hanfu")
@RequiredArgsConstructor
public class HanfuController {

    private final HanfuServiceImpl hanfuService;

    /**
     * 接口废弃
     * @GetMapping("")
     * public R getHanfuListByTypeId(@RequestParam("typeId") Integer typeId, @RequestParam("pageNum") Integer
     * pageNum, @RequestParam("pageSize") Integer pageSize){
     * return R.ok(hanfuService.getHanfuListByTypeId(typeId, pageNum, pageSize));
     * }
     */

    /**
     * wx接口
     * 
     * @param hanFuQuery
     * @return
     */
    @SaIgnore
    @PostMapping("/wx")
    public R<Page<HanfuWxVo>> getHanfuWxListByTypeId(@RequestBody HanFuQuery hanFuQuery) {
        Page<HanfuWxVo> hanfuWxListByTypeId = hanfuService.getHanfuWxListByTypeId(hanFuQuery
            .getHanFuTypeName(), hanFuQuery.getHanFuTypeId(), hanFuQuery.getHanFuName(), hanFuQuery
                .getPageNum(), hanFuQuery.getPageSize());

        return R.success(hanfuWxListByTypeId);
    }

    @SaIgnore
    @GetMapping("/wx/{id}")
    public R<HanfuWxDeVo> getByHanfuIdWxDeVo(@PathVariable("id") Integer id) {
        HanfuWxDeVo hanfuWxById = hanfuService.getHanfuWxById(id);
        hanfuWxById.setTime(DateTime.of(Long.parseLong(hanfuWxById.getTime())).toString("yyyy-MM-dd HH:mm:ss"));
        return R.success(hanfuWxById);
    }

    /**
     * 根据条件查询汉服列表
     */
    @PostMapping("/condition")
    public R<Page<HanfuVo>> getHanfuListByCondition(@RequestBody HanFuQuery hanFuQuery) {

        Page<HanfuVo> hanfuListByCondition = hanfuService.getHanfuListByCondition(hanFuQuery
            .getHanFuTypeName(), hanFuQuery.getHanFuName(), hanFuQuery.getHanFuStatus(), hanFuQuery
                .getPageNum(), hanFuQuery.getPageSize());

        hanfuListByCondition.getRecords().forEach(i -> {
            i.setUpdataTime(DateTime.of(Long.parseLong(i.getUpdataTime())).toString("yyyy-MM-dd HH:mm:ss"));
        });
        return R.success(hanfuListByCondition);
    }

    /**
     * 添加汉服接口
     */

    @PostMapping("")
    public R<Void> addHanfu(@RequestParam("img") List<MultipartFile> img,
                            @RequestParam("name") String name,
                            @RequestParam("price") Double price,
                            @RequestParam("user") String user,
                            @RequestParam("typeId") Integer typeId,
                            @RequestParam("des") String des,
                            @RequestParam("status") Integer status) {

        HanfuBo hanfuBo = new HanfuBo();
        hanfuBo.setHanfuName(name);
        hanfuBo.setHanfuPrice(price);
        hanfuBo.setHanfuTypeId(typeId);
        hanfuBo.setUpdataBy(user);
        hanfuBo.setFile(img);
        hanfuBo.setDescription(des);
        hanfuBo.setStatus(status);

        int i = hanfuService.addHanfu(hanfuBo);

        if (i != 1) {
            return R.fail("添加汉服失败");
        }

        return R.success();
    }

    /**
     *
     * 按列表删除接口
     */
    @DeleteMapping("")
    public R<Void> deleteHanfu(@RequestBody ArrayList<Integer> ids) {
        int result = hanfuService.deleteHanfu(ids);
        return result == 1 ? R.success() : R.fail("删除失败");
    }

    @PutMapping("")
    public R<Void> updateHanfu(@RequestBody HanfuDto hanfuDto) {
        return hanfuService.updateHanFu(hanfuDto) == 1 ? R.success() : R.fail("更新失败");
    }

    @PutMapping("/img")
    public R<Void> updateHanfuImg(@RequestParam("img") List<MultipartFile> img,
                                  @RequestParam("id") Integer id,
                                  @RequestParam("user") String user,
                                  @RequestParam("ossId") String newOssId) {

        HanfuBo hanfuBo = new HanfuBo();

        if (img.get(0).getSize() != 0) {
            hanfuBo.setFile(img);
        }
        hanfuBo.setUpdataBy(user);
        hanfuBo.setHanfuId(id);
        hanfuBo.setOssId(newOssId);

        int i = hanfuService.updateHanfuImg(hanfuBo);

        if (i == 0) {
            return R.fail("更新失败");
        }

        return R.success();
    }

}
